package com.example.seed.util;

import lombok.Getter;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lianbone on 2019/8/31.
 * Desc: 报错信息收集，用于发送邮件
 */
@Getter
public class MessageCollector {

    // 类型
    private static final String TYPE = "type";

    // 参数
    private static final String PARAM = "param";

    // 信息
    private static final String MESSAGE = "message";

    // 创建时间
    private static final String CREATE_DATE = "createDate";

    // 聚合数量
    private static final String COUNT = "count";


    /**
     * 标题
     */
    private String title;


    /**
     * 详情记录
     */
    private List<Map<String, String>> allData = new ArrayList<>();

    /**
     * 聚合统计
     */
    private List<Map<String, Object>> countData;

    /**
     * 实例化
     *
     * @param title
     * @return
     */
    public static MessageCollector getInstance(String title) {
        MessageCollector collector = new MessageCollector();
        collector.title = title;
        return collector;
    }

    /**
     * 增加新记录
     *
     * @param type
     * @return
     */
    public LinkSaveMap newRecord(String type) {
        return new LinkSaveMap(type);
    }

    /**
     * 聚合计算
     */
    public void calculation() {
        Map<String, Long> count = this.allData.stream().collect(Collectors.groupingBy(e -> e.get(TYPE), Collectors.counting()));
        this.countData = count.entrySet().stream().sorted((o1, o2) -> (int) (o2.getValue() - o1.getValue())).map(e -> {
            Map<String, Object> map = new HashMap<>();
            map.put(TYPE, e.getKey());
            map.put(COUNT, e.getValue());
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return this.allData.isEmpty();
    }


    public class LinkSaveMap {

        /**
         * 记录
         */
        private Map<String, String> itemData = new HashMap<>();

        /**
         * 具体参数
         */
        private Map<String, String> paramMap = new HashMap<>();


        public LinkSaveMap(String type) {
            itemData.put(TYPE, type);
        }

        /**
         * 增加参数
         *
         * @param key
         * @param value
         * @return
         */
        public LinkSaveMap putParam(String key, String value) {
            this.paramMap.put(key, value);
            return this;
        }

        /**
         * 增加信息
         *
         * @param message
         * @return
         */
        public LinkSaveMap putMessage(String message) {
            this.itemData.put(MESSAGE, message);
            return this;
        }

        /**
         * 构建，必须操作
         */
        public void build() {
            if (!this.itemData.containsKey(MESSAGE)) this.itemData.put(MESSAGE, "");
            this.itemData.put(CREATE_DATE, new DateTime().toString("YYYY-MM-dd HH:mm:ss"));
            String paramStr = this.paramMap.entrySet().stream()
                    .map((e) -> String.format("%s:%s", e.getKey(), e.getValue()))
                    .collect(Collectors.joining(";"));
            this.itemData.put(PARAM, paramStr);
            allData.add(this.itemData);
        }
    }
}
